<?php
/*******************************************************************
 *[TTTuangou] (C)2005 - 2010 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename product.logic.php $
 *
 * @Author http://www.tttuangou.net $
 *
 * @Date 2011-01-26 04:25:51 $
 *******************************************************************/ 
 


class ProductLogic
{	
	var $DatabaseHandler;
	var $Config;
	var $nowdate;
	var $nowTime;

	function ProductLogic(){
		$this->DatabaseHandler = &Obj::registry("DatabaseHandler");
		$this->CookieHandler = &Obj::registry("CookieHandler");
		$this->Config = &Obj::registry("config");
		$this->nowdate= date('Y-m-d',time());
		$this->nowTime = time();
	}
	
	function productGet($id,$city=0,$active=false){
		if ($active)
		{
			$date_sign = '>=';
		}
		else
		{
			$date_sign = '<=';
		}
		$sql_limit_city = '1';
		if ($city>0)
		{
			$sql_limit_city = 'p.city = '.intval($city);
		}
		$sql='select p.*,s.id as sellerid, s.sellername,s.sellerphone,s.selleraddress,s.sellerurl,s.sellermap,p.totalnum from '.TABLE_PREFIX.'tttuangou_product p LEFT join '.TABLE_PREFIX.'tttuangou_seller s on (p.sellerid=s.id) where p.overtime '.$date_sign.' '.$this->nowTime.' and p.id = '.intval($id).' and ('.$sql_limit_city.' or p.display = 2)';
		$query = $this->DatabaseHandler->Query($sql);
		$product=$query->GetRow();
		if(empty($product))return false;
				$product['price'] *= 1;
		$product['nowprice'] *= 1;
		if ($product['nowprice']>0)
		{
			$product['agio'] = round(10/($product['price']/$product['nowprice']),1);
		}
		else
		{
			$product['agio'] = 0;
		}
				$overtime=($product['overtime']);
		$product['time']=$overtime-time();
		$product['num'] = $product['totalnum']+$product['virtualnum'];
		$product['success'] = $product['successnum']-$product['num'];
		return $product;
	}

	function productNow($city, $all=false, $except=0){		$product=array();
		$sql_limit_city = '1';
		if ($city>0)
		{
			$sql_limit_city = 'city = '.intval($city);
		}
		$sql='select p.*,s.sellername,s.sellerphone,s.selleraddress,s.sellerurl,s.sellermap,p.totalnum from '.TABLE_PREFIX.'tttuangou_product p LEFT join '.TABLE_PREFIX.'tttuangou_seller s on (p.sellerid=s.id) where p.begintime <= '.$this->nowTime.' and p.overtime > '.$this->nowTime.' and ((p.display = 1 and '.$sql_limit_city.') or p.display = 2) ORDER BY p.order DESC,p.id DESC';
		$query = $this->DatabaseHandler->Query($sql);
		if ($all)
		{
			$products = $query->GetAll();
			if(empty($products))return false;
			$return = array();
			foreach ($products as $i=>$product)
			{
				if ($product['id'] == $except)
				{
					continue;
				}
								$product['price'] *= 1;
				$product['nowprice'] *= 1;
				$return[$i] = $product;
				if ($product['nowprice']>0)
				{
					$return[$i]['agio'] = round(10/($product['price']/$product['nowprice']),1);
				}
				else
				{
					$return[$i]['agio'] = 0;
				}
								$overtime=($product['overtime']);
				$return[$i]['time']=$overtime-time();
				$return[$i]['num'] = $return[$i]['totalnum']+$return[$i]['virtualnum'];
				$return[$i]['success'] = $return[$i]['successnum']-$return[$i]['num'];
			}
			if (count($return)<1)
			{
				return false;
			}
			return $return;
		}
		$product=$query->GetRow();
		if(empty($product))return false;
				$product['price'] *= 1;
		$product['nowprice'] *= 1;
		if ($product['nowprice']>0)
		{
			$product['agio'] = round(10/($product['price']/$product['nowprice']),1);
		}
		else
		{
			$product['agio'] = 0;
		}
				$overtime=($product['overtime']);
		$product['time']=$overtime-time();
		$product['num'] = $product['totalnum']+$product['virtualnum'];
		$product['success'] = $product['successnum']-$product['num'];
		return $product;
	}

	function productSavedMoney() 	{
		$sql = 'SELECT SUM((price-nowprice)*(virtualnum+totalnum)) AS saves FROM '.TABLE_PREFIX.'tttuangou_product WHERE overtime <= '.$this->nowTime.' AND status = 2';
		$result = $this->DatabaseHandler->Query($sql)->GetRow();
		return $result['saves'];
	}

	function productMySavedMoney()
	{
				$sql = 'SELECT o.orderid,o.productnum,p.price,p.nowprice FROM '.TABLE_PREFIX.'tttuangou_order o INNER JOIN '.TABLE_PREFIX.'tttuangou_product p ON o.productid = p.id WHERE o.userid = '.MEMBER_ID;
		$result = $this->DatabaseHandler->Query($sql)->GetAll();
		$return['count'] = count($result);
		$sum = 0;
		for($i=0; $i<count($result); $i++)
		{
			$sum += $result[$i]['productnum']*($result[$i]['price']-$result[$i]['nowprice']);
		}
		$return['saves'] = $sum;
		return $return;
	}

	function productBuysCount($id) 	{
		$sql = 'SELECT SUM(productnum) AS sums FROM '.TABLE_PREFIX.'tttuangou_order WHERE productid='.$id.' AND pay=1';
		$query = $this->DatabaseHandler->Query($sql);
		if ($query)
		{
			$result = $query->GetRow();
			return $result['sums'];
		}
		else
		{
			return 0;
		}
	}
	
	function productCheck($id,$city=''){		if($city!=''){
			$sql='select * from '.TABLE_PREFIX.'tttuangou_product where begintime <= '.$this -> nowTime.' and overtime > '.$this -> nowTime.' and id = '.$id.' and (city = '.intval($city).' or display = 2)';
		}else{
			$sql='select * from '.TABLE_PREFIX.'tttuangou_product where begintime <= '.$this -> nowTime.' and overtime > '.$this -> nowTime.' and id = '.$id;
		}
		$query = $this->DatabaseHandler->Query($sql);
		if (!$query)
		{
			return false;
		}
		$product=$query->GetRow();
				$product['price'] *= 1;
		$product['nowprice'] *= 1;
		return $product;
	}
	
	function productNum($maxnum,$id){		$sql='select sum(productnum) from '.TABLE_PREFIX.'tttuangou_order where productid = '.intval($id).' and pay = 1';
		$query = $this->DatabaseHandler->Query($sql);
		$result=$query->GetRow();
		$num=$result['sum(productnum)'];
		$surplusnum=$maxnum-$num;
		return $surplusnum;
	}
	
	function productBuyed($id){
		$sql='select count(o.orderid),p.mutibuy from '.TABLE_PREFIX.'tttuangou_order o LEFT JOIN '.TABLE_PREFIX.'tttuangou_product p ON(o.productid=p.id) WHERE o.productid = '.intval($id).' AND o.userid= '.MEMBER_ID.' AND o.pay=1 GROUP BY o.orderid';
		$query = $this->DatabaseHandler->Query($sql);
		if (!$query)
		{
			return false;
		}
		$buy = $query->GetRow();
		$counts = $buy['count(o.orderid)'];
		if ($counts == 0)
		{
			return false;
		}
		if ($buy['mutibuy']!='' && $buy['mutibuy']=='true')
		{
			return false;
		}
		return true;
	}
	
	function ProductSelled($id){
		$sql='select count(*) from '.TABLE_PREFIX.'tttuangou_order where productid = '.intval($id).' and pay = 1';
		$query = $this->DatabaseHandler->Query($sql);
		$result=$query->GetRow();
		return $result['count(*)'];
	}
	
	function productTypedit($id,$type){
		$ary=array(
			'status' => $type,
		);
		if($type==2){
			$sql='select sellerid from '.TABLE_PREFIX.'tttuangou_product where id = '.intval($id);
			$query = $this->DatabaseHandler->Query($sql); 
			$product=$query->GetRow();
			$this -> AddSellerSucNum($product['sellerid']);
		}
		$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tttuangou_product');
		$result=$this->DatabaseHandler->Update($ary,'id='.intval($id));
	}
	
	function citychange(){
		$this -> config=ConfigHandler::get('product');
				$cityary=$this->getcity();
				if($_GET['city']!=''){
			foreach($cityary as $value){
				if($value['shorthand'] == $_GET['city']){
					$this->CookieHandler->setVar('mycity',$value['cityid']);
					$city =$value['cityid'];
					break;
				};
			};
		};
				if($city == ''){
			if($this->CookieHandler->getVar('mycity')!=''){
				$city = $this->CookieHandler->getVar('mycity');
			}else{
				$set = ConfigHandler::get('product');
				$city=$set['default_city'];
			};
		};
				foreach($cityary as $value){
			if($value['cityid'] == $city){
				$cityname = $value['cityname'];
				break;
			};
		};
		$cityary=array(
			'0' => $cityary,
			'1'  => $city,
			'2'=> $cityname
		);
		return $cityary;
	}
	
	function getcity($id=''){
		if($id==''){
			$sql='select * from '.TABLE_PREFIX.'tttuangou_city where display >= 1';
			$query = $this->DatabaseHandler->Query($sql);
			$city=$query->GetAll();
		}else{
			$sql='select * from '.TABLE_PREFIX.'tttuangou_city where cityid = '.intval($id);
			$query = $this->DatabaseHandler->Query($sql);
			$city=$query->GetRow();
		}
		return $city;
	}
	
	function getcitylist(){
		$sql='select * from '.TABLE_PREFIX.'tttuangou_city';
		$query = $this->DatabaseHandler->Query($sql);
		$city=$query->GetAll();
		return $city;
	}
	
	function ProductUp(){		$sql='update '.TABLE_PREFIX.'tttuangou_product set status=0 where overtime <= '.$this -> nowTime.' and `status` = 1 ';
		$query = $this->DatabaseHandler->Query($sql); 
		return true;
	}
	function AddSellerProNum($sellerid){
		$sql='update '.TABLE_PREFIX.'tttuangou_seller set productnum = productnum + 1 where id = '.intval($sellerid);
		$query = $this->DatabaseHandler->Query($sql); 
		return true;
	}
	function DelSellerProNum($sellerid){
		$sql='update '.TABLE_PREFIX.'tttuangou_seller set productnum = productnum - 1 where id = '.intval($sellerid);
		$query = $this->DatabaseHandler->Query($sql); 
		return true;
	}
	function AddSellerSucNum($sellerid){
		$sql='update '.TABLE_PREFIX.'tttuangou_seller set successnum = successnum + 1 where id = '.intval($sellerid);
		$query = $this->DatabaseHandler->Query($sql); 
		return true;
	}
	function AddSellerTotMoney($sellerid,$money){
		$sql='update '.TABLE_PREFIX.'tttuangou_seller set money = money + '.$money.' where id = '.intval($sellerid);
		$query = $this->DatabaseHandler->Query($sql); 
	}
function delSellerTotMoney($sellerid,$money){
		$sql='update '.TABLE_PREFIX.'tttuangou_seller set money = money - '.$money.' where id = '.intval($sellerid);
		$query = $this->DatabaseHandler->Query($sql); 
	}
}

?>